from rtl_parser import RTLParser
from rtl_scanner import RTLScanner
from adder_tree import AdderTree
from exception import *

TEST_ADDER_TREE_PATH = './rtl/e3_w4_adder_tree.v'
TEST_TIMES = 256

if __name__ == '__main__':
    try:
        # Build Adder Tree
        tokens = RTLScanner(TEST_ADDER_TREE_PATH).scan()
        adder_tree = RTLParser(tokens).parse(AdderTree)
        adder_tree.show_message()

        # Optimize
        adder_tree.optimize()
        adder_tree.show_optimization_report()

        # Test
        adder_tree.random_test(TEST_TIMES)

        # TODO: Generate new RTL file
        #         

    except RTLScanException as sex:
        print(f'Scan RTL Failed: {sex}')
    except RTLParseException as pex:
        print(f'Parse RTL Failed: {pex}')
    except UnsupportCircuitException as uex:
        print(f'Build RTL Failed: {uex}')
    except AssumptionValidException as aex:
        print('Optimize Adder Tree Failed: Basic assumption invalid')
